package logging;

import java.io.IOException;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * Represents the initialization for the server logging with Log4J.
 */
public class LogSetup {
    public static final String UNKNOWN_LEVEL = "UnknownLevel";
   // private static Logger logger = Logger.getRootLogger();
    private String logdir;
    private Logger logger;
    private static LogSetup loggerSetup;
    
    /**
     * Initializes the logging for the echo server. Logs are appended to the 
     * console output and written into a separated server log file at a given 
     * destination.
     * 
     * @param logdir the destination (i.e. directory + filename) for the 
     * 		persistent logging information.
     * @throws IOException if the log destination could not be found.
    */
    public LogSetup(String logdir, Level level) throws IOException {
        this.logdir = logdir;
        initialize(level);
    }
    
    public static Logger getLogger()
    {
        if (loggerSetup == null) {
            try {
                loggerSetup = new LogSetup("logs/simulation.log", Level.ALL);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        
        return loggerSetup.getRootLogger();
    }

    private void initialize(Level level) throws IOException {
        String myPattern = "%d{ISO8601} %p [%t] %c (%F:%L) - %m%n";
        String pattern = "%d{ISO8601} %-5p [%t] %c: %m%n";
        PatternLayout layout = new PatternLayout( myPattern );
        
        FileAppender fileAppender = new FileAppender( layout, logdir, true );		

        this.logger = Logger.getRootLogger();
        ConsoleAppender consoleAppender = new ConsoleAppender(layout);
           this.logger.addAppender(consoleAppender);
           this.logger.addAppender(fileAppender);
           this.logger.setLevel(level);
    }

    public static boolean isValidLevel(String levelString) {
        boolean valid = false;

        if(levelString.equals(Level.ALL.toString())) {
                valid = true;
        } else if(levelString.equals(Level.DEBUG.toString())) {
                valid = true;
        } else if(levelString.equals(Level.INFO.toString())) {
                valid = true;
        } else if(levelString.equals(Level.WARN.toString())) {
                valid = true;
        } else if(levelString.equals(Level.ERROR.toString())) {
                valid = true;
        } else if(levelString.equals(Level.FATAL.toString())) {
                valid = true;
        } else if(levelString.equals(Level.OFF.toString())) {
                valid = true;
        }

        return valid;
    }

    public static String getPossibleLogLevels() {
           return "ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF";
    }
	
    private Logger getRootLogger(){
        return this.logger;
    }
}
